package com.lottery.im.module.login.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.OnClick;
import com.google.gson.Gson;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.LoginEntity;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.module.login.presenter.RegisterNamePresenter;
import com.lottery.im.module.login.view.RegisterNameView;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.widget.dialog.ConfirmWarnDialogBuilder;

/*
 * 注册 设置名称
 * */
public class RegisterNameFragment extends BaseSwipeFragment<RegisterNamePresenter> implements
    RegisterNameView {

  private final static String PHONE_KEY = "PHONE_KEY";
  private final static String CODE_KEY = "CODE_KEY";
  private final static String INVITE_CODE_KEY = "INVITE_CODE_KEY";
  private final static String PWD_KEY = "PWD_KEY";

  public static RegisterNameFragment create(String phone, String code, String inviteCode,
      String pwd) {
    RegisterNameFragment fragment = new RegisterNameFragment();
    Bundle b = new Bundle();
    b.putString(PHONE_KEY, phone);
    b.putString(CODE_KEY, code);
    b.putString(INVITE_CODE_KEY, inviteCode);
    b.putString(PWD_KEY, pwd);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.et_name) EditText mEtName;

  private String mPhone;
  private String mCode;
  private String mInviteCode;
  private String mPwd;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_nick_name_set;
  }

  @Override protected void initPresenter() {
    presenter = new RegisterNamePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    StatusBarHelper.setStatusBarLightMode(getActivity());
    RxBus.get().register(this);
    if (getArguments() == null) {
      popBackStack();
      return;
    } else {
      mPhone = getArguments().getString(PHONE_KEY);
      mCode = getArguments().getString(CODE_KEY);
      mInviteCode = getArguments().getString(INVITE_CODE_KEY);
      mPwd = getArguments().getString(PWD_KEY);
    }
  }

  @OnClick({ R.id.iv_back, R.id.btn_next }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        back();
        break;
      case R.id.btn_next:
        toNext();
        break;
    }
  }

  private void back() {

    ConfirmWarnDialogBuilder.getInstance(mActivity)
        .setContent(getString(R.string.confirm_give_up_reg))
        .setCancel(getString(R.string.cancel), view -> {

        })
        .setConfirm(getString(R.string.confirm), view -> popBackStack())
        .show();
  }

  /*
   *  开始注册
   * */
  private void toNext() {
    String name = mEtName.getText().toString().trim();
    if (TextUtils.isEmpty(name)) {
      showToast(R.string.hint_name);
      return;
    }
    presenter.userReg(mPhone, mPwd, mCode, mInviteCode, name);
  }

  @Override public void regSuccess(LoginEntity loginEntity) {

    MyApplication.getInstance().setToken(loginEntity.getToken());
    MyApplication.getInstance().setUserInfo(loginEntity.getUserInfo());
    SPUtils.put(mActivity, SpKeyConstant.TOKEN_KEY, loginEntity.getToken());
    SPUtils.put(mActivity, SpKeyConstant.USERINFO_KEY,
        new Gson().toJson(loginEntity.getUserInfo()));
    RxBus.get().send(RxBusCode.REG_SUCCESS_CODE);
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
